public class MyQueue {

    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode first;//队头
    public ListNode last;//队尾
    public int usedSize;//长度

    public boolean isEmpty() {
        return first == null;
        //return usedSize == 0;
    }

    public int size() {
        return usedSize;
    }

    public void offer(int val) {
        ListNode newNode = new ListNode(val);

        if (isEmpty()) {
            first = last = newNode;
        }else {
            last.next = newNode;
            newNode.prev = last;
            last = last.next;
        }
        usedSize++;
    }

    public int poll() {
        if (isEmpty()) {
            return -1;
        }
        int val = first.val;
        first = first.next;

        if (first != null) {
            first.prev = null;
        }
        usedSize--;
        return val;
    }

    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        return first.val;
    }

    public void display() {
        ListNode cur = first;
        System.out.print("[");
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.print("]");
    }
}
